jSouper.ready(function() {
	// body...
	//检查是否有历史记录用的索引，和History.length不同，这个属性只检查dotnar站点
	App.set("$Cache.page_index", -1);
	Path.on("*", function() {
		App.set("$Cache.page_index", App.get("$Cache.page_index") + 1);
		if (!(location.hash.indexOf("#default/sign_in") == 0)) {
			LS.set("$Cache.cb_url_href", location.href);
			App.set("$Cache.cb_url_href", location.href);
			document.body.scrollTop = 0;
		};
		App.set("$Cache.top.private", "");
	});

	function _check_collect_bus(collectBus) {
		var is_collect = true;
		if (jSouper.indexOf(collectBus, busInfo._id) === -1) {
			is_collect = false;
		};
		App.set("$Cache.is_collect_current_bus", is_collect);
	};
	eventManager.on("getLoginer", function() {
		//校验是否已经收藏过此商家
		var collectBus = App.get("loginer.collectBus") || [];
		_check_collect_bus(collectBus);
	});

	;
	(window.coAjaxLoginUser = function(succ_cb) {
		// alert("获取登陆者信息……"+location.pathname)

		//获取登陆用户的信息
		/*
		 * 管理员相关的页面无需登录
		 */
		var no_user_login_pages = {
			"/admin-beta.html": 1,
			"/admin-login.html": 1
		};
		if (no_user_login_pages[location.pathname]) {
			return;
		};
		// alert("开始获取"+appConfig.user.loginer)
		console.log("获取登陆者信息……", location.pathname);
		/*
		 * 获取登录者信息
		 */
		function _login_sucess(data) {
			// alert("success","用户登录成功");
			console.log("登录者信息：", data.result);
			userInfo = data.result;
			App.set("loginer", userInfo);
			//触发相关事件
			eventManager.fire("getLoginer");
			succ_cb && succ_cb();
		};

		function _login_err(errorCode) {
			var muse_login_pages = {
				"/pay": 1,
				"/cart": 1,
				"/user": 1
			};
			//未登录，不可进入个人页，强制跳转到登录页
			if (muse_login_pages[Path._current_page] || Path._current_page.indexOf("/user/") === 0) {
				myConfirm("您已退出登陆，点击是(确定)重新登陆", function() {
					Path.jump("/sign_in.html?cb_url=" + encodeURIComponent(Path._current_location.href));
				}, function() {
					Path.jump("/main.html");
				});
			};
		};

		function _cookie_login() {
			coAjax.get(appConfig.user.loginer, _login_sucess, _login_err);
		};

		window._wx_openid_login = function _wx_openid_login(openid) {
			//自动登录
			coAjax.get(appConfig.user.loginer, {
				openid: openid
			}, function() {
				alert("success", "微信授权账号自动登录成功");
				_login_sucess.apply(this, arguments);
			}, function(errorCode, xhr, errorMsg) {
				//使用OPENID登录失败，尝试使用Cookie的登录方式登录
				if (errorMsg == "refresh_token time out") {
					alert("微信授权已经过期，请重新登录");
				} else {
					alert("error", errorMsg);
					_cookie_login();
				};
			});
		};

		if (_isWX) {
			globalGet("WEIXIN_OPENID", function(openid) {
				// alert("openid:"+openid)
				if (openid) {
					_wx_openid_login(openid);
				} else {
					_cookie_login();
				};
			});
		} else {
			_cookie_login();
		};
	})();


	App.set("$Event.user_collect_bus", function() {
		if (!App.get("loginer")) {
			alert("warn", "收藏店铺前，请您先登陆！！！");
			return;
		};
		if (App.get("$Cache.is_collect_current_bus")) {
			coAjax["delete"](appConfig.user.collectBus_remove, {
				bus_id: busInfo._id
			}, function(result) {
				alert("已取消店铺收藏");
				_check_collect_bus(result.result);
			}, function(errorCode, xhr, errorMsg) {
				_check_collect_bus(App.get("loginer.collectBus"));
			});
		} else {
			coAjax.post(appConfig.user.collectBus_add, {
				bus_id: busInfo._id
			}, function(result) {
				alert("success", "店铺收藏成功");
				// App.set('$Cache.is_collect_current_bus',true);
				_check_collect_bus(result.result);
			}, function(errorCode, xhr, errorMsg) {
				// alert("error",errorMsg)
				_check_collect_bus(App.get("loginer.collectBus"));
			});
		};
	});
	App.set("$Event.top.show_bus_qrcode", function() {
		App.model.toggle("$Cache.top.is_show_bus_qrcode");
	});

	App.set("$Event.set_search_focus", function() {
		App.set("$Cache.search_focus", true);
	});
	App.set("$Event.set_search_blur", function() {
		App.set("$Cache.search_focus", false);
		App.set("$Cache.search_text", "");
	});
	App.set("$Event.open_search", function() {
		Path.jump("/searchset.html?q=" + App.get("$Cache.search_text"));
		App.set("$Cache.search_text", "");
	});

	// 直接购买
	function _get_cart_success(result) {
		var cart_detail = result.result;

		App.set("user.cart_detail", cart_detail);
		App.set("$Cache.cart.total_price", Model.Observer(function() {
			var cart_detail = App.get("user.cart_detail");
			var total_price = 0;
			cart_detail.forEach(function(cart) {
				cart.goods_info.forEach(function(goods_info) {
					total_price += goods_info.goods.price * goods_info.num;
				});
			});
			return total_price;
		}));
		alert("success", "添加成功")
	};

	function _get_cart_error(errorCode, xhr, errorMsg) {
		console.log("%c" + errorCode + ": " + errorMsg, "color:orange;font-size:14px; text-shadow: 0 1px 0 #ccc;");
		alert("error", errorMsg);
	};
	App.set("$Event.goods_list.add_to_cart", function(e, vm) {
		App.set("$Cache.top.private", 'focus');
		var cart_item = {
			goods_id: vm.get("_id"),
			num: 1
		};
		if (App.get("loginer")) {
			coAjax.post(appConfig.user.cart, cart_item, _get_cart_success, _get_cart_error);
		} else {
			var cache_cart = LS.get("$Cache.cart");
			try {
				cache_cart = JSON.parse(cache_cart);
			} catch (e) {
				console.error(e)
			};
			if (!(cache_cart instanceof Array)) {
				cache_cart = [];
			};
			cache_cart.push(cart_item);
			LS.set("$Cache.cart", JSON.stringify(cache_cart));
		};
	});
	App.set("$Event.top.stay", function() {
		App.set("$Cache.top.private", "");
	});
	App.set("$Event.top.goto_cart", function() {
		Path.jump("/cart.html");
	});
	// 注销
	App.set("$Event.top.user_login_out", function() {
		coAjax.get(appConfig.user.login_out_url, function() {
			App.set("loginer", null);
			Path.on("/user/*.html", function() {
				Path.jump("/main.html");
			});
		});
	});
	// 滚动
	$(window).scroll(function() {
		if ($(window).scrollTop() > 0) {
			App.set("$Cache.top.scroll", true);
		} else {
			App.set("$Cache.top.scroll", false);
		};
	});
});